from flask import request,jsonify
from db import db1
class ACL:
    def __init__(self):
        pass

    # 添加权限
    def add_resoutce(self, userid, resoutceid):
        sql = """select * from user_zy where userid = %d and resource_id = %d"""%(userid,resoutceid)
        res = db1.find_one(sql)
        if res:
            return jsonify({'code':10010,"mes":'已经有该资源了'})
        else:
            sql1 = """insert into user_zy(userid,resource_id) values(%d,%d)"""%(userid,resoutceid)
            db1.insert(sql1)
            return jsonify({'code':200})

    # 修改权限
    def put_resoutce(self, userid, resoutceid):
        sql = """delete from user_zy where userid = %d"""%(userid)
        db1.insert(sql)
        sql1 = """insert into user_zy(userid,resource_id) values(%d,%d)""" % (userid, resoutceid)
        db1.insert(sql1)
        return jsonify({'code': 200})

    # 删除权限
    def del_resoutce(self, userid, resoutceid):
        sql = """delete from user_zy where userid = %d""" % (userid)
        db1.insert(sql)

#登录成功掉的接口
    # 获取可操作页面
    def get_vue(self, userid):
        # 查找这个用户有哪些资源
        sql = """select r.url from user_zy as u left join resource as r on u.resource_id = r.id where u.userid = %d and r.type=1""" % int(userid)
        ress = db1.find_all(sql)
        list = []
        for i in ress:
            list.append(i['url'])
        return list

    # 获取可操作接口
    def get_flask(self, userid):
        sql = """select r.url from user_zy as u left join resource as r on u.resource_id = r.id where u.userid = %d and r.type=2""" % int(
            userid)
        ress = db1.find_all(sql)
        list = []
        for i in ress:
            list.append(i['url'])
        return list

acll = ACL()